package modules.login
{
	import flash.events.MouseEvent;
	
	import fla.ui.LoginUI;
	
	import modules.GameEvent;
	import modules.ViewManager;
	import modules.communication.Communication;
	import modules.communication.CommunicationEvent;

	public class LoginViewManager extends ViewManager
	{
		private var loginUI:LoginUI;

		public function LoginViewManager()
		{
			dispatcher.addEventListener(LoginEvent.LOGIN_SHOW, onGameEvent);
		}

		private function get connecting():Boolean
		{
			return Communication.connecting;
		}

		private function get logined():Boolean
		{
			return Login.logined;
		}

		override public function init():void
		{
			loginUI = new LoginUI();
			loginUI.x = (UIAllRefer.stage.stageWidth - loginUI.width) / 2;
			loginUI.y = (UIAllRefer.stage.stageHeight - loginUI.height) / 2;

			loginUI.infoTxt.text = "Please to connect server.";

			show();
		}

		override public function show():void
		{
			if (loginUI == null)
			{
				init();
				return;
			}

			addListeners();

			updateView();

			UIAllRefer.contentLayer.addChild(loginUI);
		}

		override public function updateView():void
		{
			loginUI.connectPathTxt.enabled = !connecting;

			loginUI.connectBtn.label = connecting ? "disconnect" : "connect";

			loginUI.usernameTxt.enabled = connecting;
			loginUI.passwordTxt.enabled = connecting;
			loginUI.loginBtn.enabled = connecting;
		}


		override public function close():void
		{
			removeListeners();

			if (loginUI.parent)
				loginUI.parent.removeChild(loginUI);
		}

		override public function addListeners():void
		{
			dispatcher.addEventListener(CommunicationEvent.CONNECT_ACK, onGameEvent);
			dispatcher.addEventListener(CommunicationEvent.DISCONNECT_ACK, onGameEvent);
			dispatcher.addEventListener(CommunicationEvent.LOGIN_ACK, onGameEvent);
			dispatcher.addEventListener(CommunicationEvent.LOGOUT_ACK, onGameEvent);

			loginUI.connectBtn.addEventListener(MouseEvent.CLICK, onClick);
			loginUI.loginBtn.addEventListener(MouseEvent.CLICK, onClick);
		}

		protected function onGameEvent(event:GameEvent):void
		{
			switch (event.type)
			{
				case LoginEvent.LOGIN_SHOW:
					show();
					break;
				case CommunicationEvent.CONNECT_ACK:
					loginUI.infoTxt.text = "Connect Success.";
					updateView();
					break;
				case CommunicationEvent.DISCONNECT_ACK:
					logined ? close() : show();
					loginUI.infoTxt.text = "Disconnected with the server.";
					updateView();
					break;
				case CommunicationEvent.LOGIN_ACK:
					if (event.data.loginSucceed)
						close();
					else
						loginUI.infoTxt.text = "Login fail.";
					break;
				case CommunicationEvent.LOGOUT_ACK:
					logined ? close() : show();
					break;
			}
		}

		protected function onClick(event:MouseEvent):void
		{
			switch (event.currentTarget)
			{
				case loginUI.connectBtn:
					if (connecting)
						dispatcher.dispatchEvent(new CommunicationEvent(CommunicationEvent.DISCONNECT_REQ));
					else
						dispatcher.dispatchEvent(new CommunicationEvent(CommunicationEvent.CONNECT_REQ, {connectPath: loginUI.connectPathTxt.text}));
					break;
				case loginUI.loginBtn:
					dispatcher.dispatchEvent(new CommunicationEvent(CommunicationEvent.LOGIN_REQ, {username: loginUI.usernameTxt.text, password: loginUI.passwordTxt.text}));
					break;
			}
		}

		override public function removeListeners():void
		{

		}
	}
}
